package com.demo.myapplication.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.demo.myapplication.R;
import com.demo.myapplication.bean.Hot;
import com.demo.myapplication.utils.CommonUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

public class HttpsFragment extends Fragment {

  private Button requestBtn;
  private EditText requestUrl;

  private TextView result;

  public static Fragment newInstance(Bundle bundle) {
    HttpsFragment fragment = new HttpsFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_https, container, false);

    requestBtn = (Button) view.findViewById(R.id.request_btn);
    requestUrl = (EditText) view.findViewById(R.id.request_url);

    result = (TextView) view.findViewById(R.id.result);

    requestBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        String url = requestUrl.getEditableText().toString();
        if (TextUtils.isEmpty(url)) {
          Snackbar.make(v, getString(R.string.please_input_url), Snackbar.LENGTH_SHORT).show();
          return;
        }
        url = CommonUtils.HTTPS_PRE + url;
        request(url);
      }
    });
    return view;
  }

  private void request(final String path) {
    new Thread(new Runnable() {
      @Override
      public void run() {
        URL url = null;
        try {
          url = new URL(path);
        } catch (MalformedURLException e) {
          e.printStackTrace();
        }
        try {
          HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
          InputStream in = connection.getInputStream();
          final StringBuilder sb = new StringBuilder();
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String read;

          while ((read = br.readLine()) != null) {
            //System.out.println(read);
            sb.append(read);
          }

          br.close();
          Gson gson = new Gson();
          final Type listType = new TypeToken<List<Hot>>() {
          }.getType();
          final List<Hot> hot = gson.fromJson(sb.toString(), listType);
          result.post(new Runnable() {
            @Override
            public void run() {
              result.setText(null);
              for (Hot item : hot) {
                result.append(String.format(getString(R.string.title), item.getTitle()));
                result.append("\n");
                result.append(Html.fromHtml(String.format(getString(R.string.content), item.getContent())));
                result.append(Html.fromHtml("<br/><span style='text-align:center;width:100%'>-----------------------------------------------------</span><br/>"));
              }
//              result.setText(sb.toString());
            }
          });
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }).start();
  }
}
